from wormbane.model.thing import Thing, MoveError
from wormbane.util.dict_ import invert_dict, module_to_dict
from wormbane.util.format import material_adjective
from wormbane.util.random_ import weighted_choice

import random

class Feature(Thing):
    is_feature = True
    kind = 'feature'
    material = 'stone'
    passable = True
    diggable = False
    fillable = False
    char = ' '

class Altar(Feature):
    is_altar = True
    kind = 'altar'
    char = '_'

class Corridor(Feature):
    is_corridor = True
    kind = 'corridor'
    diggable = True
    char = '#'

class Door(Feature):
    is_door = True
    kind = 'door'
    state = 'random'
    material = 'random'
    axis = 'random'
    desc_attrs = ('material_prefix', 'kind')

    def __init__(self, **kwargs):
        Feature.__init__(self, **kwargs)
        if self.material == 'random':
            self.material = weighted_choice(steel = 10, stone = 20, wood = 70)
        if self.state == 'random':
            if self.material == 'wood':
                self.state = weighted_choice(closed = 80, locked = 20)
            else:
                self.state = 'closed'
        if self.axis == 'random':
            self.axis = random.choice(('x', 'y'))

    @property
    def char(self):
        if self.is_open:
            return '|' if self.axis == 'x' else '-'
        else:
            return '+'

    @property
    def passable(self):
        return self.is_open

    @property
    def diggable(self):
        return self.closed and self.material == 'stone'

    @property
    def is_open(self):
        return self.state == 'open'

    @property
    def closed(self):
        return self.state != 'open'

    @property
    def locked(self):
        return self.state == 'locked'

    @property
    def unlocked(self):
        return self.state != 'locked'

    @property
    def material_prefix(self):
        if self.material != 'wood':
            yield material_adjective(self.material)

class Doorway(Feature):
    is_doorway = True
    kind = 'doorway'
    material = 'wood'
    axis = 'random'
    diggable = True
    char = '.'

    def __init__(self, **kwargs):
        Feature.__init__(self, **kwargs)
        if self.axis == 'random':
            self.axis = random.choice(('x', 'y'))

class Floor(Feature):
    is_floor = True
    kind = 'floor'
    diggable = True
    char = '.'

class Hole(Feature):
    is_hole = True
    kind = 'hole'
    char = '^'
    color = 'black'
    fillable = True

class Pit(Feature):
    is_pit = True
    kind = 'pit'
    diggable = True
    fillable = True
    char = '^'

class Stair(Feature):
    is_stair = True
    kind = 'stair'
    direction = 'random'

    def __init__(self, **kwargs):
        Feature.__init__(self, **kwargs)
        if self.direction == 'random':
            self.direction = random.choice(('up', 'down'))

    @property
    def char(self):
        return '<' if self.direction == 'up' else '>'

class Wall(Feature):
    is_wall = True
    kind = 'wall'
    axis = None
    secret = False
    passable = False

    @property
    def char(self):
        if self.axis == 'x':
            return '-'
        elif self.axis == 'y':
            return '|'
        else:
            return ' '
